import logging

from rest_framework.response import Response

from celery_tasks.main import app

from celery_tasks.sms.yuntongxun.sms import CCP

logger = logging.getLogger("django")


@app.task(name="send_sms_code")
def send_sms_code(mobile, sms_code, expire):

    ccp = CCP()
    try:
        result = ccp.send_template_sms(mobile, [sms_code, expire], 1)
    except Exception as e:
        logger.info("%s:%s发送短信失败,ERROR:%s" % (mobile, sms_code, e))
        # return Response("%s发送验证码%s失败" % (mobile, sms_code), status=503)
    else:
        if result == "0":
            logger.info("发送短信验证码成功!!!!")

        else:
            logger.warn("发送短信验证码失败!!!!")

# import logging
# from celery_tasks.main import app
#
# from celery_tasks.sms.yuntongxun.sms import CCP
# logger = logging.getLogger('django')
#
# @app.task(name='发短信任务')
# def send_sms_code(mobile, sms_code, expire):
#     # 调用第三方工具发送短信验证码
#     ccp = CCP()
#     ccp.send_template_sms(mobile, [sms_code, expire],
#                           1)
#     logger.debug({'%s:%s' % (mobile, sms_code)})
